package com.octopus.optiworks;

import junit.framework.TestCase;
import javax.annotation.Resource;
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.dataset.DefaultDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.XmlDataSet;
import org.junit.After;
import org.junit.Before;
import org.springframework.stereotype.Component;

@SuppressWarnings("restriction")
@Component
class TestBaseConfigure {

	private String driverClass = "org.hsqldb.jdbcDriver";
	private String connectionUrl = "jdbc:hsqldb:.";
	private String username = "sa";
	private String password = "";

	private boolean loadinitialdata = true;

	public boolean isLoadinitialdata() {
		return loadinitialdata;
	}

	public void setLoadinitialdata(boolean loadinitialdata) {
		this.loadinitialdata = loadinitialdata;
	}

	public String getConnectionUrl() {
		return connectionUrl;
	}

	public void setConnectionUrl(String connectionUrl) {
		this.connectionUrl = connectionUrl;
	}

	public String getDriverClass() {
		return driverClass;
	}

	public void setDriverClass(String driverClass) {
		this.driverClass = driverClass;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

public abstract class DBEnablerTestBase extends TestCase{

	@SuppressWarnings("restriction")
	@Resource
	private TestBaseConfigure testBaseConfigure;

	protected IDatabaseTester databaseTester = null;

	public String dataSetFileName() {
		return "/data_init.xml";
	}

	protected void onSetUp() {
	};

	protected void onTearDown() {
	};

	@Before
	public void setUp() throws Exception {
		// initialize your dataset here
		databaseTester = new JdbcDatabaseTester(testBaseConfigure.getDriverClass(), testBaseConfigure.getConnectionUrl(), testBaseConfigure
				.getUsername(), testBaseConfigure.getPassword());
		if (testBaseConfigure.isLoadinitialdata()) {
			IDataSet dataSet = new XmlDataSet(getClass().getResourceAsStream(dataSetFileName()));
			databaseTester.setDataSet(dataSet);
		}
		else
		{
			IDataSet dataSet = new DefaultDataSet();
			databaseTester.setDataSet(dataSet);
		}
		databaseTester.onSetup();
		onSetUp();

	}

	@After
	public void tearDown() throws Exception {
		databaseTester.onTearDown();
		onTearDown();
	}
}

// $Id$